import Vue from 'vue'
import VueRouter from 'vue-router'//, Router
import { getToken } from '@/utils/token'
// import * as VueRouter from 'vue-router'

// import Login from '@/views/Login'
// import Layout from '@/views/Layout'
// import Home from '@/views/Home'
// import User from '@/views/User'
// import Search from '@/views/Search'
// import SearchResult from '@/views/Search/SearchResult'
// import ArticleDetail from '@/views/ArticleDetail'
// import UserEdit from '@/views/User/UserEdit'
// import Chat from '@/views/Chat'
// //-总结:
// ·路由懒加载:为了让第一个页面,加载的app.js小一点,打开网页快一点
// -思路:把组件对应js，·分成若干个.js,-路由切换到哪个页面,·才去加载对应的.js文件
// ·原因: webpack分析入口时,发现router里上来就import所有页面，·所以直接打包进app.js->很大//-解决:·当路由路径匹配规则时，·才现去import引入对应的组件js文件
Vue.use(VueRouter)

// 解决导航栏中的vue-router在3.0版本以上重复点击菜单报错问题
// const originalPush = Router.prototype.push
// Router.prototype.push = function push (location) {
//   return originalPush.call(this, location).catch(err => err)
// }

const routes = [
  {
    path: '/',
    redirect: '/layout/home'
  },
  {
    path: '/login',
    component: () => import('@/views/Login'),
    beforeEnter (to, from, next) {
      if (getToken()?.length > 0 && to.path === '/login') {
        next('/layout/home')
      } else {
        next()
      }
    }
  },
  {
    path: '/layout',
    component: () => import('@/views/Layout'),
    children: [{
      path: 'home',
      component: () => import('@/views/Home'),
      meta: {
        // scrollT: 0// 保存首页离开时，滚动条的位置
        isRecord: true,
        top: 0
      }
    }, {
      path: 'user',
      component: () => import('@/views/User')
    }]
  },
  {
    path: '/search',
    component: () => import('@/views/Search')
  },
  {
    path: '/search_result/:kw',
    component: () => import('@/views/Search/SearchResult')
  },
  {
    path: '/detail',
    component: () => import('@/views/ArticleDetail')
  },
  {
    path: '/user_edit',
    component: () => import('@/views/User/UserEdit')
  },
  {
    path: '/chat',
    component: () => import('@/views/Chat')
  }
]

const router = new VueRouter({
  routes
})

// 路由-全局前置守卫(在路由发生真正跳转之前,执行此函数)
// 此函数可以决定路由是否跳转/取消/强制中断切换到别的路由
// router.beforeEach((to, from, next) => {
//   // 需求:如果你已经登录了,不要切换到登录页面
//   if (getToken()?.length > 0 && to.path === '/login') {
//     next('/layout/home')
//   } else {
//     next()
//   }
// })

export default router
